package com.sy.live.util;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

/**
 * 通过applicationContext.xml 得到Spring 的实体 Bean
 */
public class SpringBeans
{
	private static ApplicationContext context;

	/**
	 * 对外提供ApplicationContext实例
	 * 
	 * @return org.springframework.context.ApplicationContext
	 */
	public static ApplicationContext getApplicationContext()
	{
		if (null == context)
		{
			context = new ClassPathXmlApplicationContext("classpath:config/applicationContext.xml");
		}
		return context;
	}

	/**
	 * 通过bean name得到实体Bean
	 * 
	 * @param beanName
	 *            实体Bean的名称
	 * @return 返回Object
	 */
	public static Object getBeanByName(String beanName)
	{
		return getApplicationContext().getBean(beanName);
	}

	/**
	 * 通过bean type得到实体Bean
	 * 
	 * @param beanType
	 *            实体Bean的名称
	 * @return 返回Object
	 */
	public static <T> T getBeanByType(Class <T> beanType)
	{
		return getApplicationContext().getBean(beanType);
	}
}
